package org.ljj.xml.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ljj.model.UserModel;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class UserDelegate extends MultiActionController {
	
	//逻辑视图名， 通过依赖注入的方式注入 ，可配置
	private String createView;
	private String deleteView;
	private String updateView;
	private String listView;
	private String redirectToListView;
	public String getCreateView() {
		return createView;
	}
	public void setCreateView(String createView) {
		this.createView = createView;
	}
	public String getDeleteView() {
		return deleteView;
	}
	public void setDeleteView(String deleteView) {
		this.deleteView = deleteView;
	}
	public String getUpdateView() {
		return updateView;
	}
	public void setUpdateView(String updateView) {
		this.updateView = updateView;
	}
	public String getListView() {
		return listView;
	}
	public void setListView(String listView) {
		this.listView = listView;
	}
	public String getRedirectToListView() {
		return redirectToListView;
	}
	public void setRedirectToListView(String redirectToListView) {
		this.redirectToListView = redirectToListView;
	}
	
	
	
	public String create(HttpServletRequest request,HttpServletResponse response,UserModel user){
		if("GET".equalsIgnoreCase(request.getMethod()))
			return getCreateView();
		 
		return getRedirectToListView();
	}
	
	public ModelAndView update(HttpServletRequest request,HttpServletResponse response,UserModel user){
		if("GET".equalsIgnoreCase(request.getMethod())){
			ModelAndView mv = new ModelAndView();
			mv.setViewName(getUpdateView());
			//mv.addObject(getUserList().get(0));
			mv.addObject("command", getUserList().get(0));
			return mv;
		}
		 
		return new ModelAndView(getRedirectToListView());
	}
	
	
	public String delete(HttpServletRequest request,HttpServletResponse response,UserModel user){
		if("GET".equalsIgnoreCase(request.getMethod()))
			return getDeleteView();
		 
		return getRedirectToListView();
	}
	
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv = new ModelAndView();
		mv.addObject("userList", getUserList());
		mv.setViewName(getListView());
		return mv;
	}
	
	
	private List<UserModel> getUserList(){
		List<UserModel> list = new ArrayList<UserModel>();
		list.add(new UserModel(1l,"张氏","111","zs"));
		list.add(new UserModel(2l,"李氏","222","ls"));
		list.add(new UserModel(3l,"王氏","333","ww"));
		
		return list;
	}
	
	@Override
	protected String getCommandName(Object command) {
		return "command";
	}
}
